//
//  personVC.m
//  CMAccountBook
//
//  Created by 王开政 on 2018/10/31.
//  Copyright © 2018年 王开政. All rights reserved.
//

#import "personVC.h"
#import "AlertController.h"
#import "KAlteView.h"

@interface personVC ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    AlertController *alertView;
    KAlteView *alteview;
}
@property(nonatomic,strong)UITableView *personTab;

@property(nonatomic,strong)UIImageView *photoImage;

@property (nonatomic, strong) UIView *coverView;

@property (nonatomic, strong) UIButton *exitButton;
@end

@implementation personVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navig];
    
    alteview = [[KAlteView alloc]initFrame:CGRectMake(63*kfit, 306*kfit, ScreenWidth - 63*2*kfit, 138*kfit)];
    alteview.titleLabel.text = @"退出登录?";
    alteview.contentLabel.text = @"退出后不会删除历史数据!";
    [alteview.sureButton addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    [alteview.cancelButton addTarget:self action:@selector(cancelPress) forControlEvents:UIControlEventTouchUpInside];
    __weak typeof(self) weakself = self;
    alertView = [[AlertController alloc]initFrame:CGRectMake(0, ScreenHeight, ScreenWidth, 170 )];
    
    
    alertView.sheetPress = ^(NSInteger tag) {
        //100取消 101 sheet1 102 sheet2
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        if (tag ==100) {
            [weakself cancelPress];
        }
        else if (tag ==101) {
            
            //男
            [userDefault setObject:@"1" forKey:@"sex"];
//            [weakself resetSex:@"1"];
            [weakself cancelPress];
        }
        else if (tag ==102) {
//            [weakself resetSex:@"0"];
            [userDefault setObject:@"0" forKey:@"sex"];
            [weakself cancelPress];
        }
        [weakself.personTab reloadData];
    };
    [self.view addSubview:self.personTab];
    [self.view addSubview:self.exitButton];
    [self.view addSubview:self.coverView];
    [self.view addSubview:alertView];
    
    // Do any additional setup after loading the view.
}
#pragma mark-自定义导航区
-(void)navig{
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.navigationBarHidden=YES;
    __weak typeof(self) weakself = self;
    HomeNavigation*navView = [[HomeNavigation alloc]initWithTag:1 title:@"个人信息" handler:^(UIButton *sender) {
        [weakself backPress];
    }];
    [self.view addSubview:navView];
    
}
-(void)backPress{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
-(UIView *)coverView{
    
    if (!_coverView) {
        
        _coverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight )];
        _coverView.backgroundColor = [UIColor colorWithRed:127/255.0 green:127/255.0 blue:127/255.0 alpha:0.5];
        
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelPress)];
        [_coverView addGestureRecognizer:tapGesturRecognizer];
        _coverView.hidden = YES;
        
        
    }
    
    
    return  _coverView;
}

-(void)cancelPress
{
    
    
    [alteview removeFromSuperview];
    [UIView animateWithDuration:0.5 animations:^{
        self->alertView.frame = CGRectMake(0, ScreenHeight , ScreenWidth, 170);
        self.coverView.hidden = YES;
    } completion:^(BOOL finished) {
        
        
        
    }];
    
    
}
-(UIImageView *)photoImage{
    
    if (!_photoImage) {
        
        _photoImage = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth - 85,15,40,40)];
        _photoImage.layer.cornerRadius = 20;
        _photoImage.layer.masksToBounds = YES;
        _photoImage.image = [UIImage imageNamed:@"add_photo"];
        
    }
    
    return _photoImage;
}
-(UIButton *)exitButton{
    if (!_exitButton) {
        _exitButton = [[UIButton alloc]initWithFrame:CGRectMake(20*kfit, ScreenHeight - 90*kfit, ScreenWidth - 40*kfit, 45*kfit)];
        _exitButton.backgroundColor = [UIColor colorWithHexString:@"#ff5050"];
        _exitButton.layer.masksToBounds = YES;
        _exitButton.layer.cornerRadius = 5;
        [_exitButton setTitle:@"退出登录" forState:0];
        [_exitButton addTarget:self action:@selector(exitPress) forControlEvents:UIControlEventTouchUpInside];
    }
    return _exitButton;
}
-(void)exitPress{
    [UIView animateWithDuration:0.5 animations:^{
        
        self.coverView.hidden = NO;
        [self.coverView addSubview:self->alteview];
        
    } completion:^(BOOL finished) {
        
    }];
}
-(UITableView *)personTab{
    
    if (!_personTab) {
        
        _personTab = [[UITableView alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight, ScreenWidth,ScreenHeight-SafeAreaTopHeight)];
        _personTab.delegate = self;
        _personTab.dataSource = self;
        _personTab.rowHeight = 50;
        _personTab.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        _personTab.tableFooterView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
//        _personTab.scrollEnabled = NO;
        _personTab.showsHorizontalScrollIndicator = NO;
        _personTab.tableFooterView=[[UIView alloc]init];
//        _personTab.tableFooterView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        if(@available(iOS 11.0, *)){
            _personTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _personTab.estimatedRowHeight = 0;
            _personTab.estimatedSectionHeaderHeight = 0;
            _personTab.estimatedSectionFooterHeight = 0;
            
        }
    }
    
    return _personTab;
}
#pragma  make - UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    

    return 6;
            
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"personCell"];
        
        if (!cell) {
            
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"personCell"];
            
        }
        
        cell.textLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        //        cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        cell.textLabel.font = [UIFont systemFontOfSize:16*kfit];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:16*kfit];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            if (indexPath.row == 0) {
                
                                
                cell.textLabel.text = @"头像";
//                NSLog(@"头像 = %@",self.myModel.headPicture);
               
//                [cell addSubview:self.redView1];
                
//                [self.photoImage sd_setImageWithURL:[NSURL URLWithString:self.myModel.headPicture] placeholderImage:[UIImage imageNamed:@"add_photo"]];
                [cell addSubview:self.photoImage];
                
            } else if(indexPath.row == 1) {
                
                cell.textLabel.text = @"昵称";
                
                cell.detailTextLabel.text = @"遗失的痛";
//                if (self.myModel.modifyNickName == 0) {
//                    [self.redView2 removeFromSuperview];
//                }else if (self.myModel.modifyNickName==1){
//                    [cell addSubview:self.redView2];
//                }
//                [cell addSubview:self.message ];
//                self.message.text = self.myModel.nickName;
                
            }else if(indexPath.row == 2){
                
                cell.textLabel.text = @"性别";
                
                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
                NSString *sex = [userDefault valueForKey:@"sex"];
                if ([sex isEqualToString:@"1"]) {
                    cell.detailTextLabel.text = @"男";
                }else if ([sex isEqualToString:@"0"]){
                    cell.detailTextLabel.text = @"女";
                }else{
                    cell.detailTextLabel.text = @"未知";
                }
//                NSString *str = [NSString stringWithFormat:@"%@",self.myModel.sex];
                //                cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#333333"];
                
//                if ([str isEqualToString:@"0"]) {
//
//                    cell.detailTextLabel.text = @"女";
//                    [self.redView3 removeFromSuperview];
//                }else if ([str isEqualToString:@"1"]){
//
//                    cell.detailTextLabel.text = @"男";
//                    [self.redView3 removeFromSuperview];
//                }else if ([str isEqualToString:@"2"]){
//
//                    cell.detailTextLabel.text = @"未知";
//                    [cell addSubview:self.redView3];
//
//                }
                
            }else if(indexPath.row == 3){
                
                cell.textLabel.text = @"手机号";
            }else if(indexPath.row ==4){
                
                cell.textLabel.text = @"微信号";
                cell.detailTextLabel.text = @"未绑定";
            }else if(indexPath.row == 5){
                
                cell.textLabel.text = @"QQ号";
                cell.detailTextLabel.text = @"未绑定";
            }
            
        
    
    return cell;
    
}
#pragma  make - UITableViewDelegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
    
        if (indexPath.row == 0) {
                
                self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                alert.view.tintColor = [UIColor blackColor];
                //通过拍照上传图片
                UIAlertAction * takingPicAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                        
                        UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
                        imagePicker.delegate = self;
                        imagePicker.allowsEditing = YES;
                        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                        [self presentViewController:imagePicker animated:YES completion:nil];
                    }
                    
                }];
                //从手机相册中选择上传图片
                UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    
                    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
                        UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
                        imagePicker.delegate = self;
                        imagePicker.allowsEditing = YES;
                        
                        imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                        [self presentViewController:imagePicker animated:YES completion:nil];
                    }
                    
                }];
                
                UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                
                [alert addAction:takingPicAction];
                [alert addAction:okAction];
                [alert addAction:cancelAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }else if (indexPath.row == 1){
                
               
                
            }else if (indexPath.row == 2){
                
                [self changeSex];
                
                
                
            }else if (indexPath.row == 3){
                
                
                
                
            }else if (indexPath.row == 4){
                
                
                
                
            }else if (indexPath.row == 5){
                
                
                
                
            }
            
        
   
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
//返回高度，计算不同高度cell 一定要用，否则会出现偏移
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 67;
    }
    
    return 50;
}
-(void)changeSex{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.coverView.hidden = NO;
        self->alertView.frame = CGRectMake(0, ScreenHeight -160, ScreenWidth, 170);
        
    } completion:^(BOOL finished) {
        
    }];
}
#pragma marks UIImagePickerControllerDelegate
//调用系统相册及拍照功能实现方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
     UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
     self.photoImage.image = image;
    [picker dismissViewControllerAnimated:YES completion:NULL];
}
//用户取消选取时调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
